package com.human.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.human.dao.EmpMapper;
import com.human.entity.Emp;
import com.human.entity.ResultVo;
import com.human.service.IPasswordService;
import com.human.utils.RandomUtils;
import com.human.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author jzq
 * @date 2023/3/9 10:58
 */
@SuppressWarnings({"all"})
@Service
public class PasswordServiceImpl implements IPasswordService {

    @Resource
    EmpMapper empMapper;

    @Autowired
    PasswordEncoder passwordEncoder;

    //注入邮件发送服务类
    @Autowired
    JavaMailSender javaMailSender;

    //获取发件人邮箱
    @Value("${spring.mail.username}")
    private String sendEmail;

    //注入redis
    @Autowired
    RedisCache redisCache;

    @Override
    public ResultVo sendCode(String email) {
        ResultVo<Object> rv = new ResultVo<>();
        try {
            //创建邮件对象
            SimpleMailMessage message = new SimpleMailMessage();
            //定义收件人邮箱
            message.setTo(email);
            //定义发件人邮箱
            message.setFrom(sendEmail);
            //定义邮件标题
            message.setSubject("您正在修改密码");
            //定义邮件正文
            int code = RandomUtils.getRandomInt(100000, 999999);
            message.setText("验证码为:" + code);
            //发送邮件
            javaMailSender.send(message);
            rv.setSuccess(true);
            rv.setInfo("发送成功");
            //定义Redis键名
            String key = "code:" + email;
            //存入验证码并设置失效时间
            redisCache.setCacheObject(key, code + "");
            redisCache.expire(key, 3, TimeUnit.MINUTES);
        } catch (Exception e) {
            e.printStackTrace();
            rv.setSuccess(false);
            rv.setInfo("发送失败");
        }
        return rv;
    }

    @Override
    public ResultVo validateCode(Emp emp) {
        ResultVo rv = new ResultVo();
        //判断验证码是否失效
        String key = "code:" + emp.getEmail();
        Object obj = redisCache.getCacheObject(key);
        if (obj == null) {
            rv.setInfo("验证码失效");
        } else {
            if (emp.getCode().equals(obj)) {
                rv.setInfo("验证成功");
                rv.setSuccess(true);
            } else {
                rv.setInfo("验证码错误");
            }
        }
        return rv;
    }

    @Override
    public ResultVo updatePwd(Emp emp) {
        emp.setPassword(passwordEncoder.encode(emp.getPassword()));
        LambdaQueryWrapper<Emp> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Emp::getEmail, emp.getEmail());
        int i = empMapper.update(emp, lqw);
        if (i > 0) {
            return new ResultVo(true, "修改成功");
        }
        return new ResultVo(false, "修改失败");
    }

}
